/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.anny.moneystream.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.anny.moneystream.entity.AnnyMoneyStream;
import com.jeeplus.modules.anny.moneystream.service.AnnyMoneyStreamService;

/**
 * 账户金额交易表Controller
 * @author linzx
 * @version 2018-10-17
 */
@Controller
@RequestMapping(value = "${adminPath}/anny/moneystream/annyMoneyStream")
public class AnnyMoneyStreamController extends BaseController {

	@Autowired
	private AnnyMoneyStreamService annyMoneyStreamService;
	
	@ModelAttribute
	public AnnyMoneyStream get(@RequestParam(required=false) String id) {
		AnnyMoneyStream entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = annyMoneyStreamService.get(id);
		}
		if (entity == null){
			entity = new AnnyMoneyStream();
		}
		return entity;
	}



	/**
	 * 金额列表页面
	 */
	@RequiresPermissions("anny:moneystream:annyMoneyStream:list")
	@RequestMapping(value = {"list", ""})
	public String list() {
		return "modules/anny/moneystream/annyMoneyStreamList";
	}
	
		/**
	 * 金额列表数据
	 */
	@ResponseBody
	@RequiresPermissions("anny:moneystream:annyMoneyStream:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(AnnyMoneyStream annyMoneyStream, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AnnyMoneyStream> page = annyMoneyStreamService.findPage(new Page<AnnyMoneyStream>(request, response), annyMoneyStream); 
		return getBootstrapData(page);
	}

	/**
	 * 查看，增加，编辑金额表单页面
	 */
	@RequiresPermissions(value={"anny:moneystream:annyMoneyStream:view","anny:moneystream:annyMoneyStream:add","anny:moneystream:annyMoneyStream:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(AnnyMoneyStream annyMoneyStream, Model model) {
		model.addAttribute("annyMoneyStream", annyMoneyStream);
		if(StringUtils.isBlank(annyMoneyStream.getId())){//如果ID是空为添加
			model.addAttribute("isAdd", true);
		}
		return "modules/anny/moneystream/annyMoneyStreamForm";
	}

	/**
	 * 添加金额流水详情
	 * @param account
	 * @param source
	 * @param amount
	 * @param incomeFlag
	 */
	@RequestMapping(value = "MoneySaveDetails")
	public void MoneySaveDetails(String account,String source,String amount,String incomeFlag){
		AnnyMoneyStream annyMoneyStream=new AnnyMoneyStream();
		annyMoneyStream.setAccount(account);
		annyMoneyStream.setAmount(amount);
		annyMoneyStream.setSource(source);
		annyMoneyStream.setIncomeFlag(incomeFlag);
		annyMoneyStreamService.save(annyMoneyStream);

	}

/**
 * 查看金额详情
 */
	@RequestMapping(value = "selectMoneyDetails")
	public List<AnnyMoneyStream> selectMoneyDetails(String account){

		List<AnnyMoneyStream>moneyDetails=annyMoneyStreamService.getAccount(account);
		return moneyDetails;
	}



	/**
	 * 保存金额
	 */
	@RequiresPermissions(value={"anny:moneystream:annyMoneyStream:add","anny:moneystream:annyMoneyStream:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(AnnyMoneyStream annyMoneyStream, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, annyMoneyStream)){
			return form(annyMoneyStream, model);
		}
		//新增或编辑表单保存
		annyMoneyStreamService.save(annyMoneyStream);//保存
		addMessage(redirectAttributes, "保存金额成功");
		return "redirect:"+Global.getAdminPath()+"/anny/moneystream/annyMoneyStream/?repage";
	}
	
	/**
	 * 删除金额
	 */
	@ResponseBody
	@RequiresPermissions("anny:moneystream:annyMoneyStream:del")
	@RequestMapping(value = "delete")
	public AjaxJson delete(AnnyMoneyStream annyMoneyStream, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		annyMoneyStreamService.delete(annyMoneyStream);
		j.setMsg("删除金额成功");
		return j;
	}
	
	/**
	 * 批量删除金额
	 */
	@ResponseBody
	@RequiresPermissions("anny:moneystream:annyMoneyStream:del")
	@RequestMapping(value = "deleteAll")
	public AjaxJson deleteAll(String ids, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		String idArray[] =ids.split(",");
		for(String id : idArray){
			annyMoneyStreamService.delete(annyMoneyStreamService.get(id));
		}
		j.setMsg("删除金额成功");
		return j;
	}
	
//	/**
//	 * 导出excel文件
//	 */
//	@ResponseBody
//	@RequiresPermissions("anny:moneystream:annyMoneyStream:export")
//    @RequestMapping(value = "export", method=RequestMethod.POST)
//    public AjaxJson exportFile(AnnyMoneyStream annyMoneyStream, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
//		AjaxJson j = new AjaxJson();
//		try {
//            String fileName = "金额"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
//            Page<AnnyMoneyStream> page = annyMoneyStreamService.findPage(new Page<AnnyMoneyStream>(request, response, -1), annyMoneyStream);
//    		new ExportExcel("金额", AnnyMoneyStream.class).setDataList(page.getList()).write(response, fileName).dispose();
//    		j.setSuccess(true);
//    		j.setMsg("导出成功！");
//    		return j;
//		} catch (Exception e) {
//			j.setSuccess(false);
//			j.setMsg("导出金额记录失败！失败信息："+e.getMessage());
//		}
//			return j;
//    }
//
//	/**
//	 * 导入Excel数据
//
//	 */
//	@RequiresPermissions("anny:moneystream:annyMoneyStream:import")
//    @RequestMapping(value = "import", method=RequestMethod.POST)
//    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
//		try {
//			int successNum = 0;
//			int failureNum = 0;
//			StringBuilder failureMsg = new StringBuilder();
//			ImportExcel ei = new ImportExcel(file, 1, 0);
//			List<AnnyMoneyStream> list = ei.getDataList(AnnyMoneyStream.class);
//			for (AnnyMoneyStream annyMoneyStream : list){
//				try{
//					annyMoneyStreamService.save(annyMoneyStream);
//					successNum++;
//				}catch(ConstraintViolationException ex){
//					failureNum++;
//				}catch (Exception ex) {
//					failureNum++;
//				}
//			}
//			if (failureNum>0){
//				failureMsg.insert(0, "，失败 "+failureNum+" 条金额记录。");
//			}
//			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条金额记录"+failureMsg);
//		} catch (Exception e) {
//			addMessage(redirectAttributes, "导入金额失败！失败信息："+e.getMessage());
//		}
//		return "redirect:"+Global.getAdminPath()+"/anny/moneystream/annyMoneyStream/?repage";
//    }
//
//	/**
//	 * 下载导入金额数据模板
//	 */
//	@RequiresPermissions("anny:moneystream:annyMoneyStream:import")
//    @RequestMapping(value = "import/template")
//    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
//		try {
//            String fileName = "金额数据导入模板.xlsx";
//    		List<AnnyMoneyStream> list = Lists.newArrayList();
//    		new ExportExcel("金额数据", AnnyMoneyStream.class, 1).setDataList(list).write(response, fileName).dispose();
//    		return null;
//		} catch (Exception e) {
//			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
//		}
//		return "redirect:"+Global.getAdminPath()+"/anny/moneystream/annyMoneyStream/?repage";
//    }

}